package com.niit.musicserver.controller.admin;

import com.niit.musicserver.common.Result;
import com.niit.musicserver.entity.domain.Song;
import com.niit.musicserver.service.SongService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 管理员侧/ 歌单管理
 * @Author: cheng_jianzhen
 * @CreateTime： 2024/1/18 9:14
 */

@Slf4j
@RestController
public class SongController {

    @Autowired
    private SongService songService;

    /** 根据歌单id查询歌曲列表
     * @Description: 根据歌单id查询所有歌曲列表(没有分页)
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/18 10:01
     * @param: songlistId
     * @return: com.niit.musicserver.common.Result
     **/
    @GetMapping("/admin/song/songlist/{songlistId}")
    public Result getListBySonglistId(@PathVariable("songlistId") Integer songlistId) {
        log.info("根据歌单id查询歌曲列表: {}", songlistId);
        List<Song> songList = songService.getListBySonglistId(songlistId);
        return Result.success("查询成功", songList);
    }

/**
*向指定歌单(ID)当中添加新的歌曲
* @Description:向指定歌单当中添加歌曲
        场景1:从现有的歌曲列表中选择添加;
       场景2:添加新的歌曲到歌曲表中，添加后再建立歌单与歌曲的关联关系(向中间表中插入一条记录)
 *@Author: liu_ziyi
 *@createTime:2024/1/18 10:55
 **/
//请求方式:POST
//诗求地址:/admin/song/songlist/1
//请求参数:
// path:歌单id
// body:添加的歌曲信息
//请求响应数据:歌单详细信息Songlist
    @PostMapping("/admin/song/songlist/{songlistId}")
    public Result saveSongListSong(@PathVariable("songlistid") Integer songlistId, @RequestBody Song song){
        log.info("向指定歌单 （ID）当中添加新的歌曲：{}", songlistId,song);
        //向数据库中添加歌曲，并添加到指定歌单中
        int rows = songService.addSongToSonglist(songlistId,song);
        if (rows != 1){
            return Result.faile("保存失败！");
        }
        return Result.success("保存成功！");
    }

/**
 * @Description: 删除指定歌单下的某个歌曲
 * @Author: cheng_jianzhen
 * @CreateTime: 2024/1/19 9:36
 * @param: songlistId
 * @param: songId
 * @return: com.niit.musicserver.common.Result
 **/
    @DeleteMapping("admin/song/songlist/{songlist}/{songid}")
    public Result deleteSongBySonglistIdSongId(@PathVariable("songlistId") Integer songlistId,@PathVariable("songId" ) Integer songId){
        int rows = songService.deleteSongBySonglistIdSongId(songlistId,songId);
        if (rows != 1){
            return Result.faile("删除失败！");
        }
        return Result.success("删除成功！");
    }



    /**
     * @Description: 查询所有歌曲
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/19 13:25
     * @return: com.niit.musicserver.common.Result
     **/
    @GetMapping("admin/song/{songId}")
    public Result getAllSongs() {
        List<Song> songs = songService.getAllSongs();
        return Result.success("查询成功！", songs);
    }



    /**
     * @Description: 根据歌曲ID查询歌曲
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/19 13:30
     * @param: songId
     * @return: com.niit.musicserver.common.Result
     **/
    @GetMapping("admin/song/{songId}")
    public Result getSongById(@PathVariable("songId") Integer songId) {
        Song song = songService.getSongById(songId);
        if (song == null) {
            return Result.faile("未找到指定ID的歌曲！");
        }
        return Result.success("查询成功！", song);
    }



    /**
     * @Description: 基于单表添加歌曲
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/19 13:37
     * @param: song
     * @return: com.niit.musicserver.common.Result
     **/
    @PostMapping
    public Result addSong(@RequestBody Song song) {
        int rows = songService.addSong(song);
        if (rows != 1) {
            return Result.faile("添加失败！");
        }
        return Result.success("添加成功！");
    }


    /**
     * @Description: 基于单表更新歌曲信息
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/19 13:37
     * @param: songId
     * @param: updatedSong
     * @return: com.niit.musicserver.common.Result
     **/
    @PutMapping("admin/song/{songId}")
    public Result updateSong(@PathVariable("songId") Integer songId, @RequestBody Song updatedSong) {
        int rows = songService.updateSong(songId, updatedSong);
        if (rows != 1) {
            return Result.faile("更新失败！");
        }
        return Result.success("更新成功！");
    }



    /**
     * @Description: 更新歌曲信息根据歌曲ID删除歌曲
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/19 14:35
     * @param: songId
     * @return: com.niit.musicserver.common.Result
     **/
    @DeleteMapping("admin/song/{songId}")
    public Result deleteSongById(@PathVariable("songId") Integer songId) {
        int rows = songService.deleteSongById(songId);
        if (rows != 1) {
            return Result.faile("删除失败！");
        }
        return Result.success("删除成功！");
    }



}
